﻿Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class FrmSuaThongTin
    Public m_tennhap As String
    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub
    Private Sub NapLoaiDat2()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select kyhieu, tenld from LOAIDAT", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLD2.DisplayMember = "kyhieu"
        CmbLD2.ValueMember = "kyhieu"
        CmbLD2.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiDat1()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select kyhieu, tenld from LOAIDAT", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLD1.DisplayMember = "kyhieu"
        CmbLD1.ValueMember = "kyhieu"
        CmbLD1.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiBienDong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from LOAIBD", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLoaiBD.DisplayMember = "TenLoaiBD"
        CmbLoaiBD.ValueMember = "maBD"
        CmbLoaiBD.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbHuyen.DisplayMember = "T_huyen"
        CmbHuyen.ValueMember = "maH"
        CmbHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapPhong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMPhong", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        cmbPhong.DisplayMember = "Tenphong"
        cmbPhong.ValueMember = "maPhongID"
        cmbPhong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapNgan()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMNgan", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbNgan.DisplayMember = "TenNgan"
        CmbNgan.ValueMember = "maNganID"
        CmbNgan.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKe()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKe", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbKe.DisplayMember = "TenKe"
        CmbKe.ValueMember = "maKeID"
        CmbKe.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiTaiLieu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTaiLieu", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLoaiTaiLieu.DisplayMember = "TenLoaiTaiLieu"
        CmbLoaiTaiLieu.ValueMember = "maLoaiTaiLieuID"
        CmbLoaiTaiLieu.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKho()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKho", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbKho.DisplayMember = "TenKho"
        CmbKho.ValueMember = "maKhoID"
        CmbKho.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapTinhTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTinhTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbTinhTrang.DisplayMember = "TenTinhTrang"
        CmbTinhTrang.ValueMember = "maTinhTrangID"
        CmbTinhTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDoiTuong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMDOITUONG", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbDoiTuong.DisplayMember = "TenDoiTuong"
        CmbDoiTuong.ValueMember = "TenDoiTuong"
        CmbDoiTuong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewDonViHanhChinh()
        Dim tennhap As String
        tennhap = ModuleTenDangNhap.m_tennhap
        Dim TuNgay As String
        TuNgay = String.Format("{0:MM/dd/yyyy}", DateTimeTuNgay.Value)
        Dim DenNgay As String
        DenNgay = String.Format("{0:MM/dd/yyyy}", DateTimeDenNgay.Value)
        Dim sql As String
        sql = "select  bd.maBDid,  bd.NgayBD, CONVERT(char(10), bd.NgayBD, 103) AS Ngaydoi , bd.doituongid , bd.ChuSD1, bd.cho_o1, bd.toBD, " &
                                " bd.sothua1,bd.dientich1, bd.loaidat1id, bd.NoiDungBD, " &
                                "  bd.sothua2,bd.dientich2, bd.loaidat2id, " &
                                " lbd.tenloaibd , p.tenphong, ng.tenngan, k.tenke, kh.tenkho, " &
                                " tt.TenTinhTrang, tl.tenloaitailieu, bd.soluongbd, giobd" &
                                " From DMTinhTrang tt, DMPhong p, DMNgan ng, DMKe k, DMKho kh,loaiBD lbd, " &
                                " BIENDONG bd, DMTailieu tl " &
                                " WHERE bd.loaibdid = lbd.mabd and " &
                                " bd.diachihuyenid= '" & CmbHuyen.SelectedValue.ToString & "'and " &
                                " bd.diachixaid= '" & CmbXa.SelectedValue.ToString & "'and " &
                                " datediff (day, bd.ngayBD, '" & TuNgay & "') <= 0  " &
                                " and datediff (day, bd.ngayBD, '" & DenNgay & "') >= 0  " &
                                " and bd.nguoinhap = '" & tennhap & "' " &
                                " and bd.chusd1 like N'%" & ChuSD1.Text.Trim() & "%'" &
                                " and bd.maphongID = p.maphongid and " &
                                " bd.manganid = ng.manganid and " &
                                " bd.makeid = k.makeid and" &
                                " bd.makhoid = kh.makhoid and " &
                                " bd.tinhTrangid= tt.matinhtrangid and " &
                                " bd.LoaiTaiLieuid= tl.maloaiTailieuid order by ngaybd "
        DataGridViewBD.DataSource = LoadThongTin(sql)
        DataGridViewBD.Refresh()

        ob_cnn.Close()
    End Sub
    Private Sub FrmSuaThongTin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NapDonViHuyen()
        NapLoaiDat1()
        NapLoaiDat2()
        NapLoaiBienDong()
        NapLoaiTaiLieu()
        NapPhong()
        NapNgan()
        NapKe()
        NapKho()
        NapTinhTrang()
        NapDoiTuong()
        DateTimeTuNgay.Text = DateAdd(DateInterval.Day, -1, Today())
        DateTimeDenNgay.Text = Today()
        DataGridViewBD.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewBD.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub

    Private Sub CmbHuyen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbHuyen.SelectedIndexChanged
        ' load ten huyen
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & CmbHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbXa.DisplayMember = "TenXa"
        CmbXa.ValueMember = "maxa"
        CmbXa.DataSource = tbl
        ob_cnn.Close()

    End Sub

    Private Sub ButtonLoaiDat1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmLDat.Show()
    End Sub

    Private Sub ButtonLoaiDat2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmLDat.Show()
    End Sub

    Private Sub ButtonLoaiBD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmLoaiBD.Show()
    End Sub

    Private Sub DataGridViewBD_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewBD.CellClick
        Dim dong As Integer
        dong = e.RowIndex

        'Kiem tra phai dong tieu de ko
        If (dong = -1) Then
            ' MessageBox.Show("Không tìm thấy thông tin !")
            Return
        End If
        TextMaBDid.DataBindings.Clear()
        TextMaBDid.DataBindings.Add("Text", DataGridViewBD.DataSource, "maBDid")
        CmbDoiTuong.DataBindings.Clear()
        CmbDoiTuong.DataBindings.Add("text", DataGridViewBD.DataSource, "doituongid")
        ChuSD1.DataBindings.Clear()
        ChuSD1.DataBindings.Add("Text", DataGridViewBD.DataSource, "chusd1")
        DateNgayBD.DataBindings.Clear()
        DateNgayBD.Text = String.Format("{0:dd/MM/yyyy}", DataGridViewBD.Rows(dong).Cells("Ngaybd").Value.ToString())
        'DateNgayBD.DataBindings.Clear()
        'DateNgayBD.DataBindings.Add("Text", DataGridViewBD.DataSource, "NgayBD")
        TextToBD1.DataBindings.Clear()
        TextToBD1.DataBindings.Add("Text", DataGridViewBD.DataSource, "tobd")
        TextSoThua1.DataBindings.Clear()
        TextSoThua1.DataBindings.Add("Text", DataGridViewBD.DataSource, "sothua1")
        TextDienTich1.DataBindings.Clear()
        TextDienTich1.DataBindings.Add("Text", DataGridViewBD.DataSource, "dientich1")
        TextNoiDungBD.DataBindings.Clear()
        TextNoiDungBD.DataBindings.Add("Text", DataGridViewBD.DataSource, "noidungbd")
        TextDiaChiTruoc.DataBindings.Clear()
        TextDiaChiTruoc.DataBindings.Add("Text", DataGridViewBD.DataSource, "cho_o1")
        CmbLD1.DataBindings.Clear()
        CmbLD1.DataBindings.Add("Text", DataGridViewBD.DataSource, "loaidat1id")
        TextSoThua2.DataBindings.Clear()
        TextSoThua2.DataBindings.Add("Text", DataGridViewBD.DataSource, "sothua2")
        TextDienTich2.DataBindings.Clear()
        TextDienTich2.DataBindings.Add("Text", DataGridViewBD.DataSource, "dientich2")
        CmbLD2.DataBindings.Clear()
        CmbLD2.DataBindings.Add("Text", DataGridViewBD.DataSource, "loaidat2id")
        CmbLoaiBD.DataBindings.Clear()
        CmbLoaiBD.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenloaibd")
        cmbPhong.DataBindings.Clear()
        cmbPhong.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenphong")
        CmbNgan.DataBindings.Clear()
        CmbNgan.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenngan")
        CmbKe.DataBindings.Clear()
        CmbKe.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenke")
        CmbKho.DataBindings.Clear()
        CmbKho.DataBindings.Add("Text", DataGridViewBD.DataSource, "tenkho")
        CmbTinhTrang.DataBindings.Clear()
        CmbTinhTrang.DataBindings.Add("Text", DataGridViewBD.DataSource, "tentinhtrang")
        CmbLoaiTaiLieu.DataBindings.Clear()
        CmbLoaiTaiLieu.DataBindings.Add("text", DataGridViewBD.DataSource, "TenLoaitailieu")
        TruongHopBD.DataBindings.Clear()
        TruongHopBD.DataBindings.Add("text", DataGridViewBD.DataSource, "soluongbd")
        TextBoxGio.DataBindings.Clear()
        TextBoxGio.DataBindings.Add("text", DataGridViewBD.DataSource, "Giobd")
    End Sub
    Public Sub napDuLieuNhapVaodataGird()
        DataGridViewBD.Rows.Add(DateNgayBD.Text, ChuSD1.Text, TextDiaChiTruoc.Text, TextToBD1.Text, TextSoThua1.Text, TextDienTich1.Text, CmbLD1.SelectedValue.ToString, TextNoiDungBD.Text)
    End Sub
    Private Sub Update_Du_lieu()
        Try
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateNgayBD.Value)
            Dim Sql As String = ""
            Sql = " Update BIENDONG Set diachixaid = '" & CmbXa.SelectedValue.ToString & "', " &
                " diachiHuyenid = '" & CmbHuyen.SelectedValue.ToString & "'," &
                " loaidat2id = '" & CmbLD2.SelectedValue.ToString & "'," &
                " dientich2 ='" & TextDienTich2.Text & "'," &
                " sothua2 = '" & TextSoThua2.Text & "'," &
                " loaidat1id ='" & CmbLD1.SelectedValue.ToString & "', " &
                " dientich1 = '" & TextDienTich1.Text & "'," &
                " sothua1 ='" & TextSoThua1.Text & "', " &
                " toBD ='" & TextToBD1.Text & "', " &
               " loaibdid = '" & CmbLoaiBD.SelectedValue.ToString & "'," &
                " cho_o1 = N'" & TextDiaChiTruoc.Text & "'," &
                " NoiDungBD =  N'" & TextNoiDungBD.Text & "'," &
               " ChuSD1 = N'" & ChuSD1.Text & "'," &
                " maPhongId = '" & cmbPhong.SelectedValue.ToString & "' , " &
                " maNganId = '" & CmbNgan.SelectedValue.ToString & "' , " &
                " maKeId = '" & CmbKe.SelectedValue.ToString & "' , " &
                " maKhoId = '" & CmbKho.SelectedValue.ToString & "' , " &
                " TinhTrangId = '" & CmbTinhTrang.SelectedValue.ToString & "' , " &
                " LoaiTaiLieuId = '" & CmbLoaiTaiLieu.SelectedValue.ToString & "', " &
                " Doituongid= N'" & CmbDoiTuong.SelectedValue.ToString & "'," &
                " soluongbd = '" & TruongHopBD.Text & "' , " &
                " GioBD= '" & TextBoxGio.Text & "', " &
                " ngayBD = '" & NgayNhap & "' where " &
                " maBDid = '" & TextMaBDid.Text & "' "
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()

            MsgBox("Sửa thông tin thành công!")

            '    NapDataGridViewCapNhatLai()

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub XulySua()

        ''========= xu ly tên chủ sử dụng
        If ChuSD1.Text = "" Then
            MessageBox.Show("Tên chủ sử dụng không được trống ")
            Return
        End If
        ''============bây lỗi đơn vi ký hợp đồng 
        If TextDiaChiTruoc.Text = "" Then
            MessageBox.Show("Địa chỉ chủ sử dụng không được trống ")
            Return
        End If
        '==========Bẫy lỗi người ký hợp đồng
        If TextToBD1.Text <= " 0 " Or TextToBD1.Text = "" Then
            MessageBox.Show("Tờ bản đồ phải lớn hơn không hoặc không được trống ")
            Return
        End If
        '============Bẫy lỗi địa chỉ đơn vị ký hợp đồng
        If TextSoThua1.Text <= " 0 " Or TextSoThua1.Text = "" Then
            MessageBox.Show("Số hiệu thửa phải lớn hơn không hoặc được trống ")
            Return
        End If
        '==========Bẫy nội dung thực hiện
        If TextDienTich1.Text <= "0" Or TextDienTich1.Text = "" Then
            MessageBox.Show("Diện tích phải lớn hơn không (0) ")
            Return
        End If
        If TextNoiDungBD.Text = "" Then
            MessageBox.Show("Nội dung biến động không được trống ")
            Return
        Else
            QLySuaThongTin()
            Update_Du_lieu()
            NapDataGridViewDonViHanhChinh()
            Reload()
            ob_cnn.Close()
        End If

    End Sub
    Private Sub Reload()
        ChuSD1.Text = ""
        TextDiaChiTruoc.Text = ""
        TextToBD1.Text = ""
        TextSoThua1.Text = ""
        TextDienTich1.Text = ""
        TextSoThua2.Text = ""
        TextDienTich2.Text = ""
        TextNoiDungBD.Text = ""
        TextBoxGio.Text = ""
        TextMaBDid.Text = ""
        CmbLD1.SelectedIndex = 0
        CmbLD2.SelectedIndex = 0
        CmbLoaiTaiLieu.SelectedIndex = 0
        CmbLoaiBD.SelectedIndex = 0
    End Sub
    Private Sub ButtonLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLuu.Click
        XulySua()
    End Sub
    Private Sub QLySuaThongTin()
        Dim nguoisua As String
        nguoisua = ModuleTenDangNhap.m_tennhap
        Dim Sql As String = ""
        Sql = " insert into QL_Sua (maBDid, NgaySua, nguoisua) values ( '" & TextMaBDid.Text & "' ," & Format(Now.ToOADate() - 2) & ",'" & nguoisua & "') "
        Dim cmd As New SqlCommand(Sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()
    End Sub
    Private Sub QLyXoaThongTin()
        Dim nguoixoa As String
        nguoixoa = ModuleTenDangNhap.m_tennhap
        Dim Sql As String = ""
        Sql = " insert into QL_Xoa (maBDid, NgayXoa, nguoixoa) values ( '" & TextMaBDid.Text & "' ," & Format(Now.ToOADate() - 2) & ", '" & nguoixoa & "') "
        Dim cmd As New SqlCommand(Sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()
    End Sub
    Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
        If (keyData.Equals(Keys.F3)) Then
            NapDataGridViewDonViHanhChinh()
        ElseIf (keyData.Equals(Keys.F2)) Then
            XulySua()
        End If

        Return MyBase.ProcessDialogKey(keyData)
    End Function

    Private Sub Buttonlamtuoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonlamtuoi.Click


        'ChuSD1.Text = ""
        'TextDiaChiTruoc.Text = ""
        NapDataGridViewDonViHanhChinh()
    End Sub
    Private Sub Chuyen_Du_Phong()
        Try
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateNgayBD.Value)
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim Sql As String = ""
            Sql = " insert into BIENDONGXOA (loaibdid, NoiDungBD, ngayBD, chusd1, " &
                " cho_o1, toBD, sothua1, dientich1, loaidat1id, sothua2, dientich2 ," &
                " loaidat2id, diachiHuyenid, diachixaid, tinhtrangid, ngaynhap, " &
                " nguoinhap, maKhoID, maNganID, maKeID, maPhongID, DoiTuongID, " &
                " LoaiTaiLieuID, soluongbd, mabd,giobd )  values ('" & CmbLoaiBD.SelectedValue.ToString & "'," &
                " N'" & TextNoiDungBD.Text & "'," &
                " '" & NgayNhap & "'," &
                " N'" & ChuSD1.Text & "', N'" & TextDiaChiTruoc.Text & "'," &
                " '" & TextToBD1.Text & "','" & TextSoThua1.Text & "'," &
                " '" & TextDienTich1.Text & "','" & CmbLD1.SelectedValue.ToString & "'," &
                " '" & TextSoThua2.Text & "','" & TextDienTich2.Text & "'," &
                " '" & CmbLD2.SelectedValue.ToString & "'," &
                " '" & CmbHuyen.SelectedValue.ToString & "'," &
                " '" & CmbXa.SelectedValue.ToString & "'," &
                " '" & CmbTinhTrang.SelectedValue.ToString & "' ," &
                " " & Format(Now.ToOADate() - 2) & ", " &
                " '" & tennhap & "'," &
                " '" & CmbKho.SelectedValue.ToString & "', " &
                " '" & CmbNgan.SelectedValue.ToString & "'," &
                "  '" & CmbKe.SelectedValue.ToString & "'," &
                "  '" & cmbPhong.SelectedValue.ToString & "', " &
                " N'" & CmbDoiTuong.SelectedValue.ToString & "', " &
                " '" & CmbLoaiTaiLieu.SelectedValue.ToString & "', " &
                "  '" & TruongHopBD.Value.ToString & "', '" & TextMaBDid.Text & "', '" & TextBoxGio.Text & "' )"

            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub XoaDuLieu()
        If TextMaBDid.Text = "" Then
            MessageBox.Show(" Chưa có thông tin để xóa !")
            Return
        Else
            Dim sql As String
            sql = "Delete from BIENDONG where maBDid = '" & TextMaBDid.Text & " ' "
            Dim cmd As New SqlCommand(sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MessageBox.Show(" Đã xóa thành công")
        End If

    End Sub

    Private Sub ButtonXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXoa.Click
        If ChuSD1.Text = "" Then
            MessageBox.Show("Bạn chưa chọn tên chủ sử dụng để xóa ")
            Return
        End If
        If MsgBox("Bạn muốn xóa dữ liệu không ? ", MsgBoxStyle.YesNo, "Thông báo ") = MsgBoxResult.Yes Then
            QLyXoaThongTin()
            Chuyen_Du_Phong()
            XoaDuLieu()

            NapDataGridViewDonViHanhChinh()
            Reload()
        Else
            Return
        End If

    End Sub

    Private Sub ChuSD1_TextChanged(sender As Object, e As EventArgs) Handles ChuSD1.TextChanged
        'NapDataGridViewTen()
    End Sub

    Private Sub DataGridViewBD_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBD.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBD.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBD.RowHeadersWidth < Size.Width + 20 Then DataGridViewBD.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub


    Private Sub TextSoThua1_TextChanged(sender As Object, e As EventArgs) Handles TextSoThua1.TextChanged
        TextSoThua2.Text = TextSoThua1.Text
    End Sub

    Private Sub TextDienTich1_TextChanged(sender As Object, e As EventArgs) Handles TextDienTich1.TextChanged
        TextDienTich2.Text = TextDienTich1.Text
    End Sub

  
    Private Sub DataGridViewBD_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewBD.CellContentClick

    End Sub

    Private Sub CmbLD1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbLD1.SelectedIndexChanged
        CmbLD2.SelectedValue = CmbLD1.SelectedValue
    End Sub
End Class